<template>
  <div>
    <el-form :model="ruleForm" inline ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="公寓名称" prop="businessName">
        <el-input v-model="ruleForm.businessName"></el-input>
      </el-form-item>
      <el-form-item label="交租日期" prop="startDate">
        <el-date-picker
          v-model="ruleForm.startDate"
          align="right"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="endDate">
        <el-date-picker
          v-model="ruleForm.endDate"
          align="right"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">查询</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button @click="exportExcel()">导出Excel</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="contractNo"
        label="申请合同号">
      </el-table-column>
      <el-table-column
        prop="businessName"
        label="公寓名称">
      </el-table-column>
      <el-table-column
        prop="roomName"
        label="承租房间">
      </el-table-column>
      <el-table-column
        prop="tenantName"
        label="租客姓名">
      </el-table-column>
      <el-table-column
        prop="shouldMonthRent"
        label="应还保理租金">
      </el-table-column>
      <el-table-column
        label="账期">
        <template slot-scope="scope">
          <span>
  {{ scope.row.borrowedStart }} ~ {{ scope.row.borrowedEnd }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="realCollection"
        label="交租日期">
      </el-table-column>
      <el-table-column
        prop="capName"
        label="资金方">
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="ruleForm.pageNum"
      :page-sizes="[1,3, 5, 10, 100]"
      :page-size="ruleForm.pageSize"
      :page-count="pages"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

  </div>
</template>

<script>
import {getFactoringList, exportExcel} from "@/api/factoring/factoring";

export default {
  name: "index",
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      tableData: [{}],
      total: 0,
      pages: 0,
      ruleForm: {
        pageNum: 1,
        pageSize: 10,
        startDate: undefined,
        endDate: undefined,
        businessName: undefined,
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.ruleForm.pageNum = 1;
      this.list();
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    list() {
      getFactoringList(this.ruleForm).then(res => {
        console.log("列表", res)
        let msgData = res.msgData;
        this.tableData = msgData.list;
        this.total = msgData.total;
        this.pages = msgData.pages;
      })
    },
    exportExcel() {
      console.log('导出', this.ruleForm);
      exportExcel(this.ruleForm).then(res => {       // 处理返回的文件流
        console.log(res);
        const blob = new Blob([res]);       //new Blob([res])中不加data就会返回下图中[objece objece]内容（少取一层）
        const fileName = '保理查询.xlsx';             //下载文件名称
        const elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href);             // 释放URL 对象
        document.body.removeChild(elink);
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.ruleForm.pageSize = val;
      this.ruleForm.pageNum = 1;
      this.list();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.ruleForm.pageNum = val;
      this.list();
    }
  }, created() {
    this.list();
  }
}
</script>

<style scoped>

</style>
